/*
 * Copyright (c) 2008-2010, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.blogclient.options;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.panteleyev.blogclient.Options;
import org.panteleyev.swing.SwingFactory;

/**
 *
 * @author Petr Panteleyev
 */
class NetworkPanel extends OptionsDialogPanel {
    private JTextField proxyServerEdit;
    private JTextField proxyPortEdit;
    private JTextField _httpsServerEdit;
    private JTextField _httpsPortEdit;
    private JCheckBox enableProxyCheckBox;
    private JTextField proxyUsernameEdit;
    private JPasswordField proxyPasswordEdit;
    private JCheckBox proxyAuthCheckBox;


    /** Creates new form NetworkPanel */
    public NetworkPanel() {
        initComponents();
    }

    private void initComponents() {
        JPanel proxyPanel;

        setLayout(new BorderLayout());
        add(proxyPanel = SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_Y,
            enableProxyCheckBox = new JCheckBox(L10N.OPTIONS_DIALOG_ENABLE_PROXY_CHECKBOX.s()),
            SwingFactory.createPanel(BorderFactory.createEmptyBorder(3, 5, 5, 5), SwingFactory.LAYOUT.BOX_Y,
                SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                    Box.createHorizontalGlue(),
                    new JLabel("HTTP Proxy:"),
                    Box.createHorizontalStrut(3),
                    proxyServerEdit = new JTextField(20),
                    Box.createHorizontalStrut(5),
                    new JLabel(L10N.OPTIONS_DIALOG_PORT_LABEL.s()),
                    Box.createHorizontalStrut(3),
                    proxyPortEdit = new JTextField(5)
                ),
                Box.createVerticalStrut(3),
                SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                    Box.createHorizontalGlue(),
                    new JLabel("SSL Proxy:"),
                    Box.createHorizontalStrut(3),
                    _httpsServerEdit = new JTextField(20),
                    Box.createHorizontalStrut(5),
                    new JLabel(L10N.OPTIONS_DIALOG_PORT_LABEL.s()),
                    Box.createHorizontalStrut(3),
                    _httpsPortEdit = new JTextField(5)
                )
            ),
            proxyAuthCheckBox = new JCheckBox(L10N.OPTIONS_DIALOG_PROXY_AUTH_CHECKBOX.s()),
            SwingFactory.createPanel(BorderFactory.createEmptyBorder(3, 5, 5, 5), SwingFactory.LAYOUT.BOX_X,
                new JLabel(L10N.OPTIONS_DIALOG_PROXY_USER_LABEL.s()),
                Box.createHorizontalStrut(3),
                proxyUsernameEdit = new JTextField(10),
                Box.createHorizontalStrut(5),
                new JLabel(L10N.OPTIONS_DIALOG_PROXY_PASSWD_LABEL.s()),
                Box.createHorizontalStrut(3),
                proxyPasswordEdit = new JPasswordField(10)
            )
        ), BorderLayout.NORTH);

        proxyServerEdit.setMaximumSize(proxyServerEdit.getPreferredSize());
        proxyPortEdit.setMaximumSize(proxyPortEdit.getPreferredSize());
        _httpsServerEdit.setMaximumSize(_httpsServerEdit.getPreferredSize());
        _httpsPortEdit.setMaximumSize(_httpsPortEdit.getPreferredSize());

        proxyUsernameEdit.setMaximumSize(proxyUsernameEdit.getPreferredSize());
        proxyPasswordEdit.setMaximumSize(proxyPasswordEdit.getPreferredSize());

        SwingFactory.setAlignmentX(proxyPanel, LEFT_ALIGNMENT);

        enableProxyCheckBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                enableProxyAddrControls(enableProxyCheckBox.isSelected());
            }            
        });

        proxyAuthCheckBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                enableProxyAuthControls(proxyAuthCheckBox.isSelected());
            }
        });
    }

    private void enableProxyAddrControls(boolean b) {
        proxyServerEdit.setEnabled(b);
        proxyPortEdit.setEnabled(b);
        _httpsServerEdit.setEnabled(b);
        _httpsPortEdit.setEnabled(b);
    }

    private void enableProxyAuthControls(boolean b) {
        proxyUsernameEdit.setEnabled(b);
        proxyPasswordEdit.setEnabled(b);
    }

    @Override
    public void onShow() {
        enableProxyCheckBox.setSelected(Options.isProxyEnabled());
        proxyAuthCheckBox.setSelected(Options.getProxyAuth());
        proxyServerEdit.setText(Options.getProxyServer());
        proxyPortEdit.setText(Integer.toString(Options.getProxyPort()));
        _httpsServerEdit.setText(Options.getHttpsProxyServer());
        _httpsPortEdit.setText(Integer.toString(Options.getHttpsProxyPort()));
        proxyUsernameEdit.setText(Options.getProxyUser());
        proxyPasswordEdit.setText(Options.getProxyPasswd());

        enableProxyAddrControls(Options.isProxyEnabled());
        enableProxyAuthControls(Options.getProxyAuth());
    }

    @Override
    public void onOk() {
        Options.setProxyEnabled(enableProxyCheckBox.isSelected());
        Options.setProxyServer(proxyServerEdit.getText());
        Options.setProxyPort(Integer.parseInt(proxyPortEdit.getText()));
        Options.setHttpsProxyServer(_httpsServerEdit.getText());
        Options.setHttpsProxyPort(Integer.parseInt(_httpsPortEdit.getText()));
        Options.setProxyAuth(proxyAuthCheckBox.isSelected());
        Options.setProxyUser(proxyUsernameEdit.getText());
        Options.setProxyPasswd(new String(proxyPasswordEdit.getPassword()));
        Options.setSystemProxySettings();
    }

}
